package com.yuyuan.common.domain.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

@Data
public class UserDTO {
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String nickName;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String icon;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String password;

    public UserDTO() {
    }

    public UserDTO(Long id, String nickName, String icon, String password) {
        this.id = id;
        this.nickName = nickName;
        this.icon = icon;
        this.password = password;
    }

    // 将实体类转换为JSON字符串
    public String toStringJSON() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 从字符串转换为实体类
    public static UserDTO fromStringJSON(String jsonString) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(jsonString, UserDTO.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            System.out.println("Error parsing JSON string: " + jsonString);
            return null;
        }
    }
}
